package mtn.bowen.yan.server.service.impl;

import com.jfoenix.controls.JFXComboBox;
import javafx.scene.control.Alert;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.StageStyle;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.service.MenuManagementService;
import mtn.bowen.yan.server.service.PictureDishesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.*;
import java.util.Map;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-28 6:46 PM
 */
@Component
public class MenuManagementServiceImpl implements MenuManagementService {

    private final static Logger LOGGER = LoggerFactory.getLogger(MenuManagementServiceImpl.class);

    @Resource
    private PictureDishesService pictureDishesService;

    /**
     * Dialog 展示
     *
     * @param title 标题
     * @param msg   Dialog内容
     */
    @Override
    public void showDialog(String title, String msg, Alert.AlertType type) {

        Alert alert = new Alert(type);
        alert.setTitle(title);
        alert.setHeaderText(msg);
//        alert.setContentText("Ooops, there was an error!");
        alert.initStyle(StageStyle.UTILITY);
        alert.showAndWait();
    }

    @Override
    public void loadShowTime() {
        try {
            Thread.sleep(1250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 保存图片
     *
     * @param file       图片文件
     * @param dishesName 菜品名称
     */
    @Override
    public String savePicture(File file, String dishesName) {
        File allPicture = StageManager.getFoodPictures("AllPicture");
        LOGGER.info("MenuManagementServiceImpl.savePicture, allPicture: {}", allPicture.getPath() + File.separator + dishesName);
        String path = file.getPath();
        String[] split = path.split("\\.");
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream os = new FileOutputStream(allPicture.getPath() + File.separator + dishesName + "." + split[split.length - 1])) {
            byte[] bs = new byte[1024];
            int length;
            while ((length = inputStream.read(bs)) != -1) {
                os.write(bs, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return dishesName + "." + split[split.length - 1];
    }

    /**
     * 更新UI
     *
     * @param comboBox 选择种类
     */
    @Override
    public void changeUI(HBox ui, JFXComboBox comboBox) {

        ui.getChildren().clear();
        VBox vBox = pictureDishesService.deleteRecipe(comboBox);
        ui.getChildren().add(vBox);
    }

    /**
     * 删除菜品更新数据
     */
    @Override
    public void deletePicture(HBox ui, JFXComboBox comboBox) {
        ui.getChildren().clear();
        pictureDishesService.deletePicture();
        VBox vBox = pictureDishesService.deleteRecipe(comboBox);
        ui.getChildren().add(vBox);
    }

}
